package com.service.sys;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.common.base.BaseService;
import com.entity.sys.SysRole;
import com.entity.sys.SysRoleDept;
import com.entity.sys.SysRoleMenu;
import com.entity.sys.query.SysQuery;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.sys.SysRoleMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 角色信息表 服务实现类
 */
@Service
public class SysRoleService extends BaseService<SysRoleMapper, SysRole>{

    @Resource
    private SysRoleMenuService sysRoleMenuService;
    @Resource
    private SysRoleDeptService sysRoleDeptService;

    /**
     * 角色分页列表
     */
    public PageInfo<SysRole> page(SysQuery query) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        if(StrUtil.isNotBlank(query.getName())) {
            queryWrapper.like("role_name", query.getName());
        }
        if(StrUtil.isNotBlank(query.getCode())) {
            queryWrapper.like("role_key", query.getCode());
        }
        queryWrapper.orderByDesc("create_time");
        PageHelper.startPage(query.getPageNum(),query.getPageSize());
        return new PageInfo<>(baseMapper.selectList(queryWrapper));
    }

    /**
     * 获取角色id，根据角色名称
     */
    public String getIdByRoleName(String roleName){
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_name", roleName);
        SysRole role = baseMapper.selectOne(queryWrapper);
        if (role != null){
            return role.getId();
        }
        return null;
    }

    /**
     * 校验角色名称和角色代码是否唯一
     */
    public boolean checkRoleNameAndKey(String roleName, String roleKey,String id) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        if(StrUtil.isNotBlank(roleName)) {
            queryWrapper.eq("role_name", roleName);
        }
        if(StrUtil.isNotBlank(roleKey)) {
            queryWrapper.eq("role_key", roleKey);
        }
        if (StrUtil.isNotBlank(id)){
            queryWrapper.ne("id", id);
        }
        Integer count = baseMapper.selectCount(queryWrapper);
        return count>0 ? true:false;
    }

    /**
     * 新增角色菜单信息
     */
    public void insertRoleMenu(String id, String[] menuIds) {
        // 删除角色与菜单关联
        QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysRoleMenu::getRoleId,id);
        sysRoleMenuService.remove(queryWrapper);
        List<SysRoleMenu> list = new ArrayList<>();
        // 新增角色与菜单关联
        for (String menuId : menuIds) {
            list.add(new SysRoleMenu(id,menuId));
        }
        if (!list.isEmpty()){
            sysRoleMenuService.insertBatch(list);
        }
    }

    /**
     * 新增角色部门信息
     */
    public void insertRoleDept(String id, String[] deptIds) {
        // 删除角色与部门关联
        QueryWrapper<SysRoleDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysRoleDept::getRoleId,id);
        sysRoleDeptService.remove(queryWrapper);
        List<SysRoleDept> list = new ArrayList<>();
        // 新增角色与部门关联
        for (String deptId : deptIds) {
            list.add(new SysRoleDept(id,deptId));
        }
        if (!list.isEmpty()){
            sysRoleDeptService.insertBatch(list);
        }
    }
}
